package com.gomore.opple.module.cards.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.clock.scratch.ScratchView;
import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.rest.jdecard.GroupByOrderNumber;
import com.gomore.opple.service.UseJDEClicklistener;
import com.gomore.opple.utils.DateUtil;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.utils.GlideImageLoader;
import com.gomore.opple.web.cgform.jdecard.entity.TOJingdongEcardEntity;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;
import com.gomore.opple.widgets.dialog.ConfirmDialog;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/5/26.
 */
public class CardsAdapter extends CommonAdapter<GroupByOrderNumber> implements OnBannerClickListener {
    Activity mactivity;
    Context mContext;
    String mState;
    int width;
    int height;
    GroupByOrderNumber mgroupByOrderNumber;
    private UseJDEClicklistener museJDEClicklistener;

    public void setUseJDEClicklistener(UseJDEClicklistener useJDEClicklistener) {
        museJDEClicklistener = useJDEClicklistener;
    }

    public CardsAdapter(Activity activity, Context context, int layoutId, List<GroupByOrderNumber> messageList, String state) {
        super(context, layoutId, messageList);
        mactivity = activity;
        mContext = context;
        mState = state;
        width = DensityUtil.screenWith(context) - DensityUtil.dpToPx(mContext, 40);
        height = width * 290 / 620;
    }

    @Override
    protected void convert(ViewHolder holder, final GroupByOrderNumber groupByOrderNumber, final int position) {
        mgroupByOrderNumber = groupByOrderNumber;
        Banner banner = holder.getView(R.id.banner);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
        banner.setLayoutParams(layoutParams);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < groupByOrderNumber.getECards().size(); i++) {
            if (groupByOrderNumber.getECards().get(i).getDenomation() != null) {
                list.add(GlobalConstant.JDCard.getCardByMoney(groupByOrderNumber.getECards().get(i).getDenomation()));
            }
        }
        disPlayBanner(banner, list);
        if (groupByOrderNumber.getOrderNumber() != null) {
            holder.setText(R.id.bill_number, groupByOrderNumber.getOrderNumber());
        }
        if (groupByOrderNumber.getOrderTime() != null) {
            holder.setText(R.id.time, DateUtil.fomatforDate(groupByOrderNumber.getOrderTime(), DateUtil.DATE_FORMATTER_1));
        }
        TextView upload = holder.getView(R.id.upload);
        if (mState != null && mState.equals(GlobalConstant.JDEState.DISPENSED)) {
            if (groupByOrderNumber.getOrderReceiptUrl() == null) {
                upload.setVisibility(View.VISIBLE);
            } else {
                upload.setVisibility(View.GONE);
            }
        } else {
            upload.setVisibility(View.GONE);
        }
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentStart.getInstance().startTakeAndSelectPhotoActivity(mactivity, GlobalConstant.FragmentType.UNCLAIMEDFRAGMENT, position);
            }
        });
    }

    public void disPlayBanner(Banner banner, List<Integer> banners) {
        //设置banner样式
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(banners);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播，默认为true
        banner.isAutoPlay(false);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //点击事件
        banner.setOnBannerClickListener(this);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void OnBannerClick(int position) {
        if (mState == null) {
            return;
        }
        if (mState.equals(GlobalConstant.JDEState.DISPENSED)) {
            Toast.makeText(mContext, "不能查看未激活卡卷,请先上传小票激活卡卷", Toast.LENGTH_SHORT).show();
        }

        if (mState.equals(GlobalConstant.JDEState.CHECKING)) {
            Toast.makeText(mContext, "不能查看未激活卡卷,该卡卷正在审批中", Toast.LENGTH_SHORT).show();
        }

        if (mState.equals(GlobalConstant.JDEState.ACTIVED)) {
            TOJingdongEcardEntity toJingdongEcardEntity = mgroupByOrderNumber.getECards().get(position - 1);
            //如果是10,20,30的不需要弹框(设置为新卡)
            if (GlobalConstant.JDCard.isNewCard(toJingdongEcardEntity.getDenomation())) {
                return;
            }
            showClearGrayDialog(toJingdongEcardEntity);
        }
    }

    private void showClearGrayDialog(final TOJingdongEcardEntity toJingdongEcardEntity) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.clear_gray_dialog, null);
        ImageView jd_card = (ImageView) layout.findViewById(R.id.jd_card);
        RelativeLayout invisible_linear = (RelativeLayout) layout.findViewById(R.id.invisible_linear);
        TextView card_number = (TextView) layout.findViewById(R.id.card_number);
        TextView password = (TextView) layout.findViewById(R.id.password);
        ImageView go_use = (ImageView) layout.findViewById(R.id.go_use);
        final ScratchView scratch_view = (ScratchView) layout.findViewById(R.id.scratch_view);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        builder.setCancelable(true);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();

        int width = DensityUtil.screenWith(mContext);
        //设置dialog的宽高
        android.view.WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = width;//宽高可设置具体大小
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(lp);
        //设置卡卷的高度
        int jd_card_width = width - 100 - (DensityUtil.dpToPx(mContext, 40));
        int jd_card_height = jd_card_width * 173 / 620;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(jd_card_width, jd_card_height);
        jd_card.setLayoutParams(layoutParams);
        jd_card.setBackgroundResource(GlobalConstant.JDCard.getHalfCardByMoney(toJingdongEcardEntity.getDenomation()));
        //设置隐藏高度
        int scratch_height = jd_card_width * 124 / 620;
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(jd_card_width, scratch_height);
        scratch_view.setLayoutParams(layoutParams1);
        invisible_linear.setLayoutParams(layoutParams1);
        card_number.setText("卡号  " + toJingdongEcardEntity.getCardNumber());
        password.setText("密码  " + toJingdongEcardEntity.getCardPassword());
        scratch_view.setWatermark(R.mipmap.jd_clear);
        scratch_view.setEraserSize(45);
        scratch_view.setMaxPercent(70);
        scratch_view.setEraseStatusListener(new ScratchView.EraseStatusListener() {
            @Override
            public void onProgress(int percent) {

            }

            @Override
            public void onCompleted(View view) {
                scratch_view.setVisibility(View.GONE);
            }
        });
        //去使用
        go_use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确定框
                ConfirmDialog.onNoOnclickListener onNoOnclickListener = null;
                ConfirmDialog.onYesOnclickListener onYesOnclickListener = null;
                final String title = "提示";
                String message = "确定使用吗?";
                onYesOnclickListener = new ConfirmDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        museJDEClicklistener.useJDE(toJingdongEcardEntity.getCardNumber());
                        alertDialog.dismiss();
                    }
                };
                onNoOnclickListener = new ConfirmDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        DialogUtils.cancel();
                    }
                };
                DialogUtils.showConfirmDialog(mactivity, onYesOnclickListener, onNoOnclickListener, title, message);
            }
        });
    }
}
